/* ----------------------------------------------------------------------------
* File: thread_wrapper.h
*
* Desc: Interface for thread
*
* Created: 27/06/2011, by Alexey Korshunov
* ---------------------------------------------------------------------------- */
#ifndef _THREAD_WRAPPER_H_
#define _THREAD_WRAPPER_H_

#include "error.h"
#include <windows.h>


namespace Common
{
	class ThreadWrapper
	{
	public:
		virtual ~ThreadWrapper();

		// Run logic
		virtual Common::Error Run() = 0;
		// Query exit status
		bool QueryExitStatus(DWORD dwDuration = 0);

	protected:
		ThreadWrapper();

		// Start thread
		bool StartThread();
		// Wait thread
		void WaitThread();
		// Stop thread
		void StopThread();

	private:
		static DWORD __stdcall __ThreadProxyProc(LPVOID lpParam);
		virtual void BeforeThread();
		virtual void AfterThread();

	protected:
		HANDLE m_hThread;
		HANDLE m_hEventThreadStart;
		HANDLE m_hEventThreadOver;
		HANDLE m_hEventUserAbort;
	};
}

#endif // _THREAD_WRAPPER_H_